/**
 * Created by pankajmishra on 14/02/15.
 */
define(['jquery', 'mustache','module/template/homeTemplate'], function($, mustache, template){
    return function(){

        $.getJSON('/user/getMe').
            done(function(data){
                if(!data.hasOwnProperty('groups') || data.groups.length < 2){
                    var dom =  $( template['home-essential']);
                    dom.dialog({
                        hide: { effect: "explode", duration: 100 },
                        modal: true,
                        minWidth : 700
                    });

                    dom.find('.js_submit').once('click', function(){

                        var apartment = dom.find('#apartment').val(), block = dom.find('#block').val(),
                            flatNo = dom.find('#flatNo').val().trim();

                        if(apartment == 0){
                          //  dom.find('#blockError').show();
                            return;
                        }
                        if(block == 0){
                            dom.find('#blockError').show();
                            return;
                        }
                        if(flatNo == ''){
                            dom.find('#flatNoError').show();
                            return;
                        }
                        $.ajax({url : '/user/saveHomeAddress',
                            type : "POST",
                            data : {apartment : apartment, block:block , flatNo: flatNo }}).
                            done(function(data){
                                dom.dialog( "close" );
                            });
                    });

                }
            });
    }
});